package com.cgsrp.cgsrp_user_province.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.kms.aliyun.exceptions.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cgsrp.cgsrp_user_province.service.SendSMSService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class SendSMSServiceImpl implements SendSMSService {

    @Override
    public boolean sendSMS(String phone, String templateCode, String code) {
        // 连接阿里云
        DefaultProfile profile=DefaultProfile.getProfile("cn-hangzhou","LTAI5tRX1Ey2ZdtS2W9wqSUx","aWb3yz3tmcJS90fCXtvWEqgLQIkf5c");
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");// 不要动
        request.setVersion("2017-05-25");// 不要动
        request.setAction("SendSMS");
        // 自定义参数（手机号，验证码，签名，模板）
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName","cgsrp毕设使用");
        request.putQueryParameter("TemplateCode",templateCode);
        // 构建一个短信验证码
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        // 节省资本，需要用的时候再解开
       try{
           CommonResponse response = client.getCommonResponse(request);
           System.out.println(response.getData());
           return response.getHttpResponse().isSuccess();
       } catch (com.aliyuncs.exceptions.ClientException e){
           e.printStackTrace();
       }
//        return false;
        return true;
    }
}
